package com.roger.query.service.impl;

import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;

import com.roger.query.service.QueryService;
import com.roger.vo.RangeQueryParams;

/**
 *范围查询
 * @author luoweiyi
 *
 */
public class RangeQueryServiceImpl<T> implements QueryService<RangeQueryParams<T>>{

	@Override
	public Query getQuery(RangeQueryParams<T> params) {
		Query query = null;
		
		T start = params.getStart();
		T end = params.getEnd();
		
		if(null == start && null == end)
			return null;
		
		T temp = null != start?start:end;
		
		if(temp instanceof String){
			query =  TermRangeQuery.newStringRange(params.getField(),(String)start,(String)end,params.isIncludeLower(),params.isIncludeUpper());
		}else if(temp instanceof Integer){
			query =  NumericRangeQuery.newIntRange(params.getField(),(Integer)start,(Integer)end,params.isIncludeLower(),params.isIncludeUpper());
		}else if(temp instanceof Long){
			query =  NumericRangeQuery.newLongRange(params.getField(),(Long)start,(Long)end,params.isIncludeLower(),params.isIncludeUpper());
		}else if(temp instanceof Float){
			query =  NumericRangeQuery.newFloatRange(params.getField(), (Float)start, (Float)end, params.isIncludeLower(), params.isIncludeUpper());
		}else if(temp instanceof Double){
			query =  NumericRangeQuery.newDoubleRange(params.getField(), (Double)start, (Double)end, params.isIncludeLower(), params.isIncludeUpper());
		}else{
			System.out.println("The params type is error!!!");
		}
		return query;
	}

}	
